package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.Impl.TaskFormServiceImpl;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.TaskFromService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;
@Component
public class CityHouseCheck implements IExcelVerifyHandler {
    /**
     * 错误信息
     */
    private StringBuilder msg;

    /**
     * 省市区地区map
     */
    private HashMap<String, List> map;

    @Autowired
    private TaskFromService taskFromService;

    public  static CityHouseCheck cityHouseCheck;

    @PostConstruct
    public void init(){
        cityHouseCheck=this;
        cityHouseCheck.taskFromService=this.taskFromService;
    }

    public static Map<String, Object> getUserInfo(String userCode){
        return cityHouseCheck.taskFromService.getUserInfo(userCode);
    }

    public CityHouseCheck(HashMap map) {
        this.map = map;
    }

    @Override
    public ExcelVerifyHandlerResult verifyHandler(Object o) {
        // 默认为true
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        msg = new StringBuilder();
        boolean hasMsg = isHasMsg(o);
        if (hasMsg) {
            result.setSuccess(false);
            result.setMsg(msg.toString());
        }
        return result;
    }

    /**
     * 是否有错误消息
     */
    private boolean isHasMsg(Object o) {
        if (isFieldHasMsg(o)) {
            msg.append("填写错误");
            return true;
        }
        return false;
    }

    /**
     * 校验字段是否市下拉列表中的字典值
     */
    public boolean isFieldHasMsg(Object o) {
        boolean flag = false;

        // 所属省份
        String province = getFieldValueByFieldName("belongtoProvince", o);
        if (province != null && !map.containsKey(province)) {
            msg.append("所属省份(填写全名称,检查省市县关系),");
            flag = true;
        } else {
            String city = getFieldValueByFieldName("belongtoCity", o);
            // 所属地级市
            if (!"北京市".equals(province) && !"天津市".equals(province) && !"上海市".equals(province) && !"重庆市".equals(province)) {
                List<String> cities = map.get(province);
                if (city != null && cities != null && !cities.contains(city)) {
                    msg.append("所属地级市(填写全名称,省市关系不匹配),");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("belongtoArea", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配),");
                        flag = true;
                    }
                }
            }
            // 所属地级市为直辖市
            else {
                if (city != null && !province.equals(city)) {
                    msg.append("所属地级市(填写全名称,直辖市应与省份同名)");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("area", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配)");
                        flag = true;
                    }
                }
            }
        }

        // 被调查建筑分类(下拉选项)
        String bulidingTypeField = getFieldValueByFieldName("bulidingType", o);
        Map bulidingType=new HashMap<>();
        bulidingType.put("既有","");
        bulidingType.put("新增","");
        bulidingType.put("已拆除","");
        boolean bulidingTypeBoo = bulidingType.containsKey(bulidingTypeField);
        if (bulidingTypeField != null && !bulidingTypeBoo) {
            msg.append("被调查建筑分类(下拉选项),");
            flag = true;
        }

        // 结构类型(下拉选项)
        String structureTypeField = getFieldValueByFieldName("structureType", o);
        Map structureType=new HashMap<>();
        structureType.put("砖混","");
        structureType.put("底层混凝土柱-上部砖混","");
        structureType.put("钢筋混凝土","");
        structureType.put("砖木","");
        structureType.put("钢结构","");
        structureType.put("木结构","");
        structureType.put("其他","");
        boolean structureTypeBoo = structureType.containsKey(structureTypeField);
        if (structureTypeField != null && !structureTypeBoo) {
            msg.append("结构类型(下拉选项),");
            flag = true;
        }

        // 建造时间(下拉选项)
        String buildingTimeField = getFieldValueByFieldName("buildingTime", o);
        Map buildingTime=new HashMap<>();
        buildingTime.put("70s以前","");
        buildingTime.put("70s","");
        buildingTime.put("80s","");
        buildingTime.put("90s","");
        buildingTime.put("00s","");
        buildingTime.put("10s","");
        boolean buildingTimeBoo = buildingTime.containsKey(buildingTimeField);
        if (buildingTimeField != null && !buildingTimeBoo) {
            msg.append("建造时间(下拉选项),");
            flag = true;
        }

        // 原设防烈度(下拉选项)
        String oldFortificationIntensityField = getFieldValueByFieldName("oldFortificationIntensity", o);
        Map oldFortificationIntensity=new HashMap<>();
        oldFortificationIntensity.put("未设防","");
        oldFortificationIntensity.put("6度(0.05g)","");
        oldFortificationIntensity.put("7度(0.10g)","");
        oldFortificationIntensity.put("7度(0.15g)","");
        oldFortificationIntensity.put("8度(0.20g)","");
        oldFortificationIntensity.put("8度(0.30g)","");
        oldFortificationIntensity.put("9度(0.40g)","");
        boolean oldFortificationIntensityBoo = oldFortificationIntensity.containsKey(oldFortificationIntensityField);
        if (oldFortificationIntensityField != null && !oldFortificationIntensityBoo) {
            msg.append("原设防烈度(下拉选项),");
            flag = true;
        }

        // 有无明显可见的裂缝、变形、倾斜等(下拉选项)
        String isDamageField = getFieldValueByFieldName("isDamage", o);
        Map isDamage=new HashMap<>();
        isDamage.put("有","");
        isDamage.put("无","");
        boolean isDamageBoo = isDamage.containsKey(isDamageField);
        if (isDamageField != null && !isDamageBoo) {
            msg.append("有无明显可见的裂缝、变形、倾斜等(下拉选项),");
            flag = true;
        }

        //是否进行过抗震加固/抗震加固时间(下拉选项)
        String isSeismicReinforceField = getFieldValueByFieldName("isSeismicReinforce", o);
        Map isSeismicReinforce=new HashMap<>();
        isSeismicReinforce.put("未加固","");
        isSeismicReinforce.put("80s","");
        isSeismicReinforce.put("90s","");
        isSeismicReinforce.put("00s","");
        isSeismicReinforce.put("10s","");
        boolean isSeismicReinforceBoo = isSeismicReinforce.containsKey(isSeismicReinforceField);
        if (isSeismicReinforceField != null && !isSeismicReinforceBoo) {
            msg.append("是否进行过抗震加固/抗震加固时间(下拉选项),");
            flag = true;
        }

        //初步结论(下拉选项)
        String resultField = getFieldValueByFieldName("result", o);
        Map result=new HashMap<>();
        result.put("未抗震设防","");
        result.put("抗震设防能力不足","");
        result.put("基本符合抗震设防要求","");
        boolean resultBoo = result.containsKey(resultField);
        if (resultField != null && !resultBoo) {
            msg.append("初步结论(下拉选项),");
            flag = true;
        }

        //疑似存在安全隐患(下拉选项)
        String suspectedSafetyHazardField = getFieldValueByFieldName("suspectedSafetyHazard", o);
        Map suspectedSafetyHazard=new HashMap<>();
        suspectedSafetyHazard.put("是","");
        suspectedSafetyHazard.put("否","");
        boolean suspectedSafetyHazardBoo = suspectedSafetyHazard.containsKey(suspectedSafetyHazardField);
        if (suspectedSafetyHazardField != null && !suspectedSafetyHazardBoo) {
            msg.append("疑似存在安全隐患(下拉选项),");
            flag = true;
        }

        //采集员账号
        String userCode = getFieldValueByFieldName("userCode", o);
        Map<String, Object> userInfo = getUserInfo(userCode);
        if (userCode!=null && userInfo.get("userId") == null ) {
            msg.append("采集员账号,");
            flag = true;
        }
        return flag;
    }





    /**
     * 根据属性名获取属性值
     *
     * @param fieldName 属性名
     * @param object    对象
     * @return
     */
    private String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
